package com.cy.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Description: 添加描述
 * date: 2021/3/19 0019 17:14
 *
 * @author Mr.xiaoshi
 * @since JDK 1.8
 */
public class ServletUtil {
    /**
     * 方法描述: 获取请求中的Request对象
     * @param:
     * @return: HttpServletRequest 类型的request
     * @author: Mr.XiaoShi
     * @date: 2021/3/19 0019
     */
    public static HttpServletRequest getRequest(){
        return getRequestAttributes().getRequest();
    }
    /**
     * 方法描述: 获取响应对象
     * @param: []
     * @return: javax.servlet.http.HttpServletResponse
     * @author: Mr.XiaoShi
     * @date: 2021/3/19 0019
     */
    public static HttpServletResponse getResponse(){
        return getRequestAttributes().getResponse();
    }/**
     * 方法描述: 获取会话
     * @param: []
     * @return: javax.servlet.http.HttpSession
     * @author: Mr.XiaoShi
     * @date: 2021/3/19 0019
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }
    /**
     * 方法描述: 获取请求上下文属性
     * @param: []
     * @return: org.springframework.web.context.request.ServletRequestAttributes
     * @author: Mr.XiaoShi
     * @date: 2021/3/19 0019
     */
    public static ServletRequestAttributes getRequestAttributes(){
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }
}
